define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form){

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'financing/accepts/index',
                    //add_url: 'users/user/add',
                    //edit_url: 'users/user/edit',
                    //del_url: 'users/user/del',
                    //for_url: 'financing/accepts/for',
                    multi_url: 'financing/accepts/multi',
                    table: 'accepts',
                }
            });

            var table = $("#table");

            //console.log($('#table').bootstrapTable('getAllSelections'));
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                //sortName: 'user.id',
                commonSearch: true,
                searchFormVisible: true,
                visible: false,
                showToggle: false,
                showColumns: false,
                search:false,
                showExport: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('序号'), formatter: function(value, row, index){
                                return ++index;
                            }, operate: false},
                        {field: 'code', title: __('服务单号'), operate: 'LIKE'},
                        {field: 'info.name', title: __('就诊人'), operate: 'LIKE'},
                        {field: 'info.sex', title: __('性别'), operate: false},
                        {field: 'age', title: __('年龄'), operate: false},
                        {field: 'receivable', title: __('应收金额（元）'), operate: false},
                        {field: 'reality_cost', title: __('实收金额（元）'), operate: false},
                        {field: 'medic_name', title: __('专家'), operate: false},
                        {field: 'payee', title: __('收款人'), operate: false},
                        //{field: 'image', title: __('头像'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        //{field: 'draw', title: __('抽奖次数'), operate: false, sortable: true},
                        {field: 'refund', title: __('退款金额（元）'), operate: false},
                        {field: 'is_refund', title: __("退款状态"), searchList: {"0":__('未退款'),"1":__('已退款')}, formatter: Table.api.formatter.status},
                        {field: 'status', title: __("状态"), searchList: {"0":__('待缴费'),"1":__('部分缴费'),"2":__('缴费完成'),"3":__('已关闭')}, formatter: Table.api.formatter.status},
                        //{field: 'userss.name', title: __('上级姓名'), operate: 'LIKE', sortable: true},
                        {field: 'create_time', title: __('创建时间'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        //{field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}

                        {field: 'operate', title: __('Operate'), table: table,
                            events: Table.api.events.operate,
                            buttons: [{
                                name: 'detail',
                                extend: 'data-area=\'["100%", "100%"]\'',
                                text: __('Detail'),
                                //icon: 'fa fa-list',
                                classname: 'btn btn-info btn-xs btn-detail btn-dialog',
                                url: 'financing/accepts/detail'
                            }, {
                                name: '退款',
                                //extend: 'data-area=\'["100%", "100%"]\'',
                                text: __('退款'),
                                //icon: 'fa fa-leaf',
                                classname: 'btn btn-xs btn-primary btn-dialog',
                                url: 'financing/accepts/editp',
                                hidden:function(row){
                                    if (row.is_refund == 1){
                                        return true;
                                    }else {
                                        if((row.status == 0) || (row.status == 3)){
                                            return true;
                                        }
                                    }

                                }
                            },
                                {
                                    name: '关闭',
                                    text: __('关闭'),
                                    title: __('关闭'),
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    //icon: 'fa fa-magic',
                                    confirm: '是否确认关闭？',
                                    url: 'financing/accepts/close',
                                    success: function (data, ret) {
                                        //Layer.alert(ret.msg + ",返回数据：" + JSON.stringify(data));
                                        //如果需要阻止成功提示，则必须使用return false;

                                        $(".btn-refresh").trigger("click");

                                        //console.log(ret);
                                    },
                                    error: function (data, ret) {
                                        console.log(data, ret);
                                        Layer.alert(ret.msg);
                                        return false;
                                    },
                                    hidden:function(row){
                                        if((row.status == 3) || (row.is_refund == 1)){
                                            return true;
                                        }

                                    }
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
            //添加以下部分
            // $(document).on("click",".btn-for",function(){
            //
            //     var ids = Table.api.selectedids(table);
            //     //var url = options.extend.for_url;
            //     var url = 'users/user/for';
            //     console.log(url);
            //
            //     if (url.indexOf("{ids}") !== -1){
            //         url = Table.api.replaceurl(url,{ids:ids.length > 0 ? ids.join(","):0});
            //     }
            //     Fast.api.open(url,'123',$(this).data() || {})
            // });

        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        for: function () {
            Controller.api.bindevent();
        },
        fors: function () {
            Controller.api.bindevent();
        },

        // api: {
        //     bindevent: function () {
        //         Form.api.bindevent($("form[role=form]"));
        //     }
        // }

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"),function (data,ret) {
                    //console.log(ret);

                    if (ret.code == 1){
                        //提示及关闭当前窗口
                        var msg = ret.hasOwnProperty("msg") && ret.msg !== "" ? ret.msg : __('Operation completed');
                        parent.parent.parent.Toastr.success(msg);
                        parent.parent.parent.$(".btn-refresh").trigger("click");
                        parent.parent.$(".btn-refresh").trigger("click");
                        //var index = parent.Layer.getFrameIndex(window.name);

                        //parent.parent.Layer.close(index);

                        parent.parent.Layer.closeAll();
                        parent.parent.parent.Layer.closeAll();

                        // setTimeout(function(){  //使用  setTimeout（）方法设定定时2000毫秒
                        //     window.parent.parent.location.reload();
                        // },1500);
                        return false;
                    }

                });

            }
        }

    };
    return Controller;
});